<template>
  <view >
    <!-- 使用自定义搜索组件 -->
    <my-search @click="handleSearch"></my-search>
    
    <!-- 滚动区域 -->
    <view class="scroll-container">
      <!-- 左侧一级分类 -->
      <scroll-view scroll-y="true" class="left-scroll-view" style="height: calc(100vh - 50px);">
        <block v-for="(item ,index) in cateList" :key="item.cat_id">
          <view :class="['left-scroll-item' ,active===index?'active':'']" @click="activeChanged(index)">{{item.cat_name}}
          </view>
        </block>
      </scroll-view>
      <!-- 右侧二级分类和三级分类 -->
      <scroll-view :scroll-top="scroolTop" class="right-scroll-view" scroll-y="true" style="height: calc(100vh - 50px);">
        <view class="cate-level2" v-for="c2 in cateLevel2" :key="c2.cat_id">
          <!-- 二级分类 -->
          <view class="cate-level2-name">
            /{{c2.cat_name}}/
          </view>
          <!--三级分类 -->
          <view class="cate-lv3-list">
            <view class="cate-lv3-item" v-for="c3 in c2.children" :key="c3.cat_id" @click="handleGoodsList(c3)">
              <!-- 图片 -->
              <image class="c3-img" :src="c3.cat_icon" />
              <!-- 文本 -->
              <text>{{c3.cat_name}}</text>
            </view>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
  
</template>

<script>
  import {
    escook
  } from '../../utils/escook/index.js'
  import {
    showMsg
  } from '../../utils/interact/index.js'
  import mixinBadge from '../../mixins/tabbar-badge.js'
  
  export default {
    mixins: [mixinBadge],
    //数据
    data() {
      return {
        cateList: [],
        active: 0,
        cateLevel2: [],
        scroolTop: 0
      };
    },

    //生命周期函数
    onLoad() {
      this.getCateList();
    },
    //方法
    methods: {
      //获取分类列表数据
      async getCateList() {
        const {
          data: res
        } = await escook.get('api/public/v1/categories');
        if (res.meta.status !== 200) return showMsg();
        this.cateList = res.message;
        this.cateLevel2 = this.cateList[0].children;
      },

      //事件处理函数
      activeChanged(index) {
        this.active = index;
        this.cateLevel2 = this.cateList[index].children;
        this.scroolTop = this.scroolTop === 0 ? 1 : 0;
      },

      handleGoodsList(c3) {
        uni.navigateTo({
          url: '/subpkg/goods_list/goods_list?cid=' + c3.cat_id
        });
      },
      
      //搜索框点击事件的回调
      handleSearch(){
        uni.navigateTo({
          url:'/subpkg/search/search'
        })
      }

    },

  }
</script>

<style lang="scss">
  .scroll-container {
    display: flex;

    // 左侧导航区
    .left-scroll-view {
      width: 40vw;

      //每一项的样式
      .left-scroll-item {
        background-color: #f3f3f3;
        text-align: center;
        font-size: 16px;
        line-height: 43px;

        //选中时的样式
        &.active {
          background-color: #fff;
          position: relative;

          &::before {
            content: ' ';
            display: block;
            width: 3px;
            height: 30px;
            background-color: #ff0000;
            position: absolute;
            top: 0;
            left: 0;
            transform: translateY(20%);
          }
        }
      }
    }
  }

  //右侧二级分类样式
  .cate-level2-name {
    font-size: 18px;
    font-weight: bold;
    text-align: center;
    padding: 5px 0;
  }

  //三级分类大容器
  .cate-lv3-list {
    display: flex;
    flex-wrap: wrap;

    //三级分类每一项的父容器
    .cate-lv3-item {
      width: 33.33%;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      margin-bottom: 20rpx;

      //图片的样式
      .c3-img {
        width: 158rpx;
        height: 158rpx;
      }

      //文本的样式
      text {
        font-size: 16px;
      }
    }
  }
</style>